package main.objet;

import main.Coordonnees;

import java.awt.Color;
import java.awt.Graphics;

public class Balle extends Objet3D {

    int LONGEUR = 600;
    int LARGEUR = 400;
    private int rebond = 0;
    private boolean out = false;

    public Balle() {
	super();
    }

    public Balle(Coordonnees position, Coordonnees vitesse) {
	super(position, vitesse);
    }

    public void setRebond(int rebond) {
	this.rebond = rebond;
    }

    public int getRebond() {
	return rebond;
    }

    public void setOut(boolean out) {
	this.out = out;
    }

    public boolean getOut() {
	return out;
    }

    public void seDeplacer() {
	int x = this.getPosition().getX();
	int y = this.getPosition().getY();
	int z = this.getPosition().getZ();
	int Vx = this.getVitesse().getX();
	int Vy = this.getVitesse().getY();
	int Vz = this.getVitesse().getZ();
	int newX = x + Vx;
	int newY = y + Vy;
	int newZ = z + Vz;

	if (!((newZ == 0) & (Vz == 0))) {
	    Vz = Vz - 1;
	}

	if (((newX + newZ / 2 + 7) < LONGEUR) & (newX - newZ / 4 >= 0)
		& ((newY + newZ / 2 + 7) < LARGEUR) & (newY - newZ / 4 >= 0)) {
	    this.setPosition(newX, newY, newZ);
	    this.setVitesse(Vx, Vy, Vz);
	} else if ((newX + newZ / 2 + 7) >= LONGEUR) {
	    newX = 1200 - newX - newZ - 10;
	    Vx = -Vx;
	    this.setPosition(newX, newY, newZ);
	    this.setVitesse(Vx, Vy, Vz);
	} else if (newX - newZ / 4 < 0) {
	    newX = -newX + newZ / 4;
	    Vx = -Vx;
	    this.setPosition(newX, newY, newZ);
	    this.setVitesse(Vx, Vy, Vz);
	} else if ((newY + newZ / 2 + 7) >= LARGEUR) {
	    newY = 800 - newY - newZ - 10;
	    Vy = -Vy;
	    this.setPosition(newX, newY, newZ);
	    this.setVitesse(Vx, Vy, Vz);
	} else if (newY - newZ / 4 < 0) {
	    newY = -newY + newZ / 4;
	    Vy = -Vy;
	    this.setPosition(newX, newY, newZ);
	    this.setVitesse(Vx, Vy, Vz);
	}
	if (newZ <= 0) {
	    newZ = -newZ;
	    if (-Vz - 3 >= 0) {
		Vz = -Vz - 3;
	    } else {
		Vz = 0;
	    }

	    this.setPosition(newX, newY, newZ);
	    this.setVitesse(Vx, Vy, Vz);
	}
    }

    public void seDessiner(Graphics g) {

	g.setColor(Color.yellow);
	g.fillOval(this.getPosition().getX() - this.getPosition().getZ() / 4,
		this.getPosition().getY() - this.getPosition().getZ() / 4,
		7 + this.getPosition().getZ() / 2, 7 + this.getPosition()
			.getZ() / 2);
	g.setColor(Color.black);
	g.drawOval(this.getPosition().getX() - this.getPosition().getZ() / 4,
		this.getPosition().getY() - this.getPosition().getZ() / 4,
		7 + this.getPosition().getZ() / 2, 7 + this.getPosition()
			.getZ() / 2);
    }
}